package com.conversationboard.formatter;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.conversationboard.config.Configuration;

/**
 * Remove images from text and replace them with a hyperlink.
 * 
 * It needs to cope with image URLs that look like:
 * 
 * <img src='url'></img> <img src='url'/>
 * 
 * It also needs to account for any amount of whitespace between these tags,
 * e.g. < img src = 'url' > </img>
 * 
 * The approach is like this... just remove all tags that look like </img>
 * completely, then match on the remaining tags, ignoring whether it has a '/'
 * or not.
 * 
 * The need to cope with the above forms is really only relevant for older
 * images, since new ones are all inserted in the form <img src='url'></img>,
 * since the code makes sure of this.
 * 
 * @author Keith Watson
 * 
 */

public class ImageHider {

	private static final Pattern imageTag = Pattern.compile("<\\s*img.*?src\\s*=\\s*['\"](.*?)['\"].*?>", (Pattern.CASE_INSENSITIVE | Pattern.DOTALL));
	private static final Pattern imageLinkTag = Pattern.compile("<a\\s*href.*?^(</a>)<\\s*img.*?src\\s*=\\s*['\"](.*?)['\"].*?>.*?</a>", (Pattern.CASE_INSENSITIVE | Pattern.DOTALL));
	private static final Pattern imageEndTag = Pattern.compile("</img>", (Pattern.CASE_INSENSITIVE | Pattern.DOTALL));
	private static final Pattern youTubeObject = Pattern.compile("<object width=\"425\" height=\"355\"><param name=\"movie\" value=\"(.*?&hl\\=en)\".*?</object>");


	private static String substitute(String body, boolean admin, Pattern pattern) {

		/* Performance: rough attempt at avoiding work if there are no images or youtube clips. */

		if (!(body.contains("img") || body.contains("IMG") || body.contains("<object"))) {
			return body;
		}

		/* Substitute YouTube embeds for a link */

		Matcher youTubeMatcher = youTubeObject.matcher(body);
		body = youTubeMatcher.replaceAll("<a href=\"$1\">$1</a>");

		/* Remove all </img> tags */

		Matcher endTagMatcher = imageEndTag.matcher(body);
		body = endTagMatcher.replaceAll("");

		/* We're now possibly in an invalid state, but remove the remaining. Now cope with the remaining image tags */

		Matcher matcher = pattern.matcher(body);

		body = matcher.replaceAll("<a href=\"$1\">$1</a>");

		return body;

	}


	public static String substituteImagesForLinks(String body, boolean admin, boolean forceSubstitution, boolean userPrefersToHide) {

		if (forceSubstitution) {
			body = substitute(body, admin, imageLinkTag);
			return substitute(body, admin, imageTag);
		}

		if (admin) {

			if (userPrefersToHide) {
				body = substitute(body, admin, imageLinkTag);
				return substitute(body, admin, imageTag);
			}

			if (!Configuration.getInstance().isAdminsCanSeePictures()) {
				body = substitute(body, admin, imageLinkTag);
				return substitute(body, admin, imageTag);
			}

			return body;

		} else {
			if (userPrefersToHide) {
				body = substitute(body, admin, imageLinkTag);
				return substitute(body, admin, imageTag);
			} else {
				return body;
			}
		}

	}


	public static void main(String[] args) {

		String input = "<object width=\"425\" height=\"355\"><param name=\"movie\" value=\"http://www.youtube.com/v/3DEnwUAzPG4&hl=en\"></param><param name=\"wmode\" value=\"transparent\"></param><embed src=\"http://www.youtube.com/v/3DEnwUAzPG4\" type=\"application/x-shockwave-flash\" wmode=\"transparent\" width=\"425\" height=\"355\"></embed></object><object width=\"425\" height=\"355\"><param name=\"movie\" value=\"http://www.youtube.com/v/3DEnwUAzPG4&hl=en\"></param><param name=\"wmode\" value=\"transparent\"></param><embed src=\"http://www.youtube.com/v/3DEnwUAzPG4\" type=\"application/x-shockwave-flash\" wmode=\"transparent\" width=\"425\" height=\"355\"></embed></object><object width=\"425\" height=\"355\"><param name=\"movie\" value=\"http://www.youtube.com/v/3DEnwUAzPG4&hl=en\"></param><param name=\"wmode\" value=\"transparent\"></param><embed src=\"http://www.youtube.com/v/3DEnwUAzPG4\" type=\"application/x-shockwave-flash\" wmode=\"transparent\" width=\"425\" height=\"355\"></embed></object>";

		input = ImageHider.substituteImagesForLinks(input, true, true, true);

		System.out.println(input);

	}

}
